<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '活动添加' : '活动编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
      @opened="opened"
    >
      {{ user }}
      <el-form :model="user">
        <el-form-item label="活动名称" label-width="120px">
          <el-input v-model="user.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" label-width="120px">
          <el-date-picker
            v-model="time"
            type="datetimerange"
            align="right"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['12:00:00', '08:00:00']"
            @change="change"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类" label-width="120px">
          <el-select
            v-model="user.first_cateid"
            placeholder="请选择"
            @change="changeTwo"
          >
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" label-width="120px">
          <el-select
            v-model="user.second_cateid"
            placeholder="请选择"
            @change="changeGoods"
          >
            <el-option
              v-for="item in secondCateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" label-width="120px">
          <el-select v-model="user.goodsid" placeholder="请选择">
            <el-option
              v-for="item in newList"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="this.info.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { catelist, seckadd, seckedit, seckinfo } from "../../../http/api";

import { errorAlert, successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  data() {
    return {
      user: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
      time: [],
      secondCateList: [],
      newList: [],
    };
  },
  computed: {
    ...mapGetters({
      list: "cate/list",
      goodsList: "goods/list",
    }),
  },
  methods: {
    ...mapActions({
      reqList: "cate/reqList",
      reqGoodsList: "goods/reqList",
      reqSeckList: "seckill/reqList",
    }),
    //添加
    add() {
      seckadd(this.user).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.empty();
          this.info.isshow = false;
          this.reqSeckList();
        }
      });
    },
    change() {
      console.log(this.time);
      this.user.begintime = this.time[0].getTime();
      this.user.endtime = this.time[1].getTime();
    },
    async changeTwo() {
      this.user.second_cateid = "";
      this.user.goodsid = "";
      let res = await catelist({ pid: this.user.first_cateid });
      if (res.data.code == 200) {
        this.secondCateList = res.data.list ? res.data.list : [];
      }
    },
    //请求商品数据，获取所有的商品
    changeGoods() {
      this.user.goodsid = "";
      this.reqGoodsList(true);
    },
    //取消
    cancel() {
      this.info.isshow = false;
    },
    //清空
    empty() {
      this.user = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      };
      this.time = [];
      this.secondCateList = [];
      this.newList = [];
    },

    //获取一个数据
    async getOne(id) {
      let res = await seckinfo({ id });
      this.$set(this.time, 0, new Date(res.data.list.begintime));
      this.$set(this.time, 1, new Date(res.data.list.endtime));
      console.log(this.time);
      this.user = res.data.list;
      this.user.id = id;
      let res1 = await catelist({ pid: this.user.first_cateid });
      if (res1.data.code == 200) {
        this.secondCateList = res1.data.list ? res1.data.list : [];
      }
    },
    opened() {
      if (!this.info.isAdd) {
        this.reqGoodsList(true);
        if (this.goodsList) {
          this.goodsList.forEach((item) => {
            if (item.second_cateid == this.user.second_cateid) {
              this.newList.push(item);
            }
          });
        }
        console.log(this.newList);
      }
    },
    update() {
      seckedit(this.user).then((res) => {
        if ((res.data.code = 200)) {
          successAlert(res.data.msg);
          this.info.isshow = false;
          this.empty();
          //通知父组件刷新表格
          this.reqSeckList();
        }
      });
    },
    //如果是编辑窗口取消，清空数据，添加窗口的取消，不需要清空
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
  },
  //获取第二分类下的商品
  watch: {
    goodsList(newValue, oldValue) {
      console.log(newValue);
      this.newList = [];
      newValue.forEach((item) => {
        if (item.second_cateid == this.user.second_cateid) {
          this.newList.push(item);
        }
      });
      console.log(this.newList);
    },
  },
  mounted() {
    this.reqList();
  },
};
</script>

<style scoped>
.upload {
  position: relative;
  width: 150px;
  height: 150px;
  border: 1px dashed #333;
}
.upload p {
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 150px;
}
.upload input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  z-index: 100;
  cursor: pointer;
}
.upload img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>